package com.lkd.http.controller;

import com.lkd.entity.TaskEntity;
import com.lkd.entity.TaskStatusTypeEntity;
import com.lkd.http.vo.CancelTaskVo;
import com.lkd.http.vo.TaskReportInfoVO;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.service.TaskStatusTypeService;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class TaskController extends BaseController{

    @Autowired
    private TaskStatusTypeService statusTypeService;
    @Autowired
    private TaskService taskService;

    @GetMapping("/task/allTaskStatus")
    public List<TaskStatusTypeEntity> statusTypeList(){
        return statusTypeService.list();
    }

    /**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @param innerCode
     * @param userId
     * @param taskCode
     * @param status
     * @param isRepair
     * @param start
     * @param end
     * @return
     */
    @GetMapping("/task/search")
    public Pager<TaskEntity> search(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
                                    @RequestParam(value = "pageSize",required = false,defaultValue = "10") Long pageSize,
                                    @RequestParam(value = "innerCode",required = false,defaultValue = "") String innerCode,
                                    @RequestParam(value = "userId",required = false,defaultValue = "") Integer userId,
                                    @RequestParam(value = "taskCode",required = false,defaultValue = "") String taskCode,
                                    @RequestParam(value = "status",required = false,defaultValue = "") Integer status,
                                    @RequestParam(value = "isRepair",required = false,defaultValue = "") Boolean isRepair,
                                    @RequestParam(value = "start",required = false,defaultValue = "") String start,
                                    @RequestParam(value = "end",required = false,defaultValue = "") String end){
       return taskService.search(pageIndex,pageSize,innerCode,userId,taskCode,status,isRepair,start,end);
    }

    /**
     * 查询详情
     * @param taskId
     * @return
     */
    @GetMapping("/task/taskInfo/{taskId}")
    public TaskEntity getByTaskId(@PathVariable(name = "taskId")Long taskId){
        return taskService.getById(taskId);
    }

    /**
     * 创建工单
     * @param taskViewModel
     * @return
     */
    @PostMapping("/task/create")
    public Boolean create(@RequestBody TaskViewModel taskViewModel)
    {
//        当前登录认id
        Integer assignorId = this.getUserId();
        return taskService.create(taskViewModel,assignorId);
    }

    /**
     * 接受工单
     * @param taskId
     * @return
     */
    @GetMapping("/task/accept/{taskId}")
    public Boolean accept(@PathVariable(name = "taskId")Long taskId){

//        登录用户id
        Integer loginUserId = this.getUserId();
        return taskService.accept(taskId,loginUserId);
    }

    /**
     * 取消，拒绝工单
     * @param taskId
     * @return
     */
    @PostMapping("/task/cancel/{taskId}")
    public Boolean cancel(@PathVariable(name = "taskId")Long taskId,
                          @RequestBody CancelTaskVo cancelTaskVo){

//        登录用户id
        Integer loginUserId = this.getUserId();
        return taskService.cancel(taskId,loginUserId,cancelTaskVo);
    }

    /**
     * 完成工单
     * @param taskId
     * @return
     */
    @GetMapping("/task/complete/{taskId}")
    public Boolean complete(@PathVariable(name = "taskId")Long taskId){

//        登录用户id
        Integer loginUserId = this.getUserId();
        return taskService.complete(taskId,loginUserId);
    }

    @GetMapping("task/taskReportInfo/{start}/{end}")
    public List<TaskReportInfoVO> taskReportInfo(@PathVariable(name = "start")String start,
                                                 @PathVariable(name = "end")String end){
        return taskService.taskReportInfo(start,end);
    }
}
